function hfile = fmr_SaveSTC(hfile, totio)
% FMR::SaveSTC  - save memory-bound STC data
%
% FORMAT:       [fmr] = fmr.SaveSTC([totio]);
%
% Input fields:
%
%       totio       if given and true convert data to transio object
%
% No output fields.
%
% Note: for STCData fields being transio, the function does nothing.

% Version:  v0.7b
% Build:    7083115
% Date:     Aug-31 2007, 3:26 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'fmr')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
if nargin < 2 || ...
   ~islogical(totio) || ...
    isempty(totio)
    totio = false;
else
    totio = totio(1);
end

% try saving the STC
sc = bvqxfile_getscont(hfile.L);
bc = sc.L;
[dfpn{1:3}] = fileparts(sc.F);
dsf = bc.DataStorageFormat;
if ~any([1, 2] == dsf)
    error( ...
        'BVQXfile:InvalidObject', ...
        'Unsupported DataStorageFormat field.' ...
    );
end
nov = bc.NrOfVolumes;
nos = bc.NrOfSlices;
rsx = bc.ResolutionX;
rsy = bc.ResolutionY;
dds = [rsx, rsy, nov, nos];

% create filename template
if any(dfpn{3}(2:end) == upper(dfpn{3}(2:end)))
    stcx = '.STC';
else
    stcx = '.stc';
end
stctf = [dfpn{1} '/' bc.Prefix];

% for old format
if dsf == 1
    for tsc = 1:numel(bc.Slice)
        if istransio(bc.Slice(tsc).STCData)
            continue;
        end
        try
            stcf = sprintf('%s%d%s', stctf, tsc, stcx);
            stci = fopen(stcf, 'w', 'ieee-le');
            if stci < 1
                error('FILEOPEN_FAILED');
            end
            fwrite(stci, dds(1:2), 'uint16');
            fwrite(stci, bc.Slice(tsc).STCData(:), 'uint16');
            fclose(dwii);
        catch
            error( ...
                'BQVXfile:FileNotWritable', ...
                'File not writable: ''%s''.', ...
                dwif ...
            );
        end
    
        % reload as transio?
        if totio
            try
                bc.Slice(bc).STCData = ...
                    transio(stctf, 'ieee-le', 'uint16', 4, dds(1:3));
            catch
                error( ...
                    'BVQXfile:transioError', ...
                    'Error reopening the written file as transio: %s.', ...
                    lasterr ...
                );
            end
        end
    end
    
    % set content back
    bvqxfile_setcont(hfile.L, bc);
    
% for new format
elseif ~istransio(bc.Slice.STCData)
    try
        stci = fopen([stctf stcx], 'w', 'ieee-le');
        if stci < 1
            error('FILEOPEN_FAILED');
        end
        fwrite(stci, bc.Slice.STCData(:), 'uint16');
        fclose(dwii);
    catch
        error( ...
            'BQVXfile:FileNotWritable', ...
            'File not writable: ''%s''.', ...
            dwif ...
        );
    end

    % reload as transio?
    if totio
        try
            bc.Slice.STCData = ...
                transio([stctf stcx], 'ieee-le', 'uint16', 0, dds);
        catch
            error( ...
                'BVQXfile:transioError', ...
                'Error reopening the written file as transio: %s.', ...
                lasterr ...
            );
        end
    end
    
    % set content back
    bvqxfile_setcont(hfile.L, bc);
end
